package com.example.parkingsharing.ui.components.login

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.material.OutlinedTextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.example.parkingsharing.R
import com.example.parkingsharing.data.ParkingSharingStrings
import com.example.parkingsharing.data.User
import com.example.parkingsharing.ui.theme.Orange400
import com.example.parkingsharing.utils.getRandomString

/**
 * 简单的登录页面
 */
@Composable
fun LoginScreen(
    modifier: Modifier = Modifier,
    navController: NavController,
    backStackEntry: NavBackStackEntry,
){
    var inputText by remember() { mutableStateOf("") }
//    val constraintSet = decoupledConstraints()
    var isLogin = backStackEntry.arguments?.getBoolean("isLogin")
    Column(
        modifier = modifier
            .fillMaxSize(),
        verticalArrangement = Arrangement.SpaceEvenly,
    ) {
        Spacer(modifier = Modifier
            .height(300.dp)
            .fillMaxWidth())
        // TODO("屏幕旋转的适配")
//    BoxWithConstraints(modifier = Modifier.fillMaxSize()) {
//        val constraintSet = if (maxWidth < maxHeight) {
//            decoupledConstraints(marginBottom = 35.dp, marginEnd = 10.dp)
//        } else {
//            decoupledConstraints(marginBottom = 15.dp, marginEnd = 15.dp)
//        }
//    }
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
        ){
            OutlinedTextField(
                value = inputText,
                onValueChange = { inputText = it },
                label = { Text(text = "UID") },
                placeholder = { Text(text = getRandomString(User.USERID_LENGTH)) },
                trailingIcon = {
                    Image(
                        painter = painterResource(id = R.drawable.ic_baseline_account_circle_24),
                        contentDescription = null,
                        modifier = Modifier
                            .clickable { inputText = "" }
                            .size(20.dp))//按钮点击事件
                },
            )
        }
        Row(
            modifier = Modifier
                .fillMaxSize()
                .padding(100.dp),
            horizontalArrangement = Arrangement.Center,
        ) {
            Button(
                onClick = {
                    // TODO("实现登录，登录成功跳转到用户信息界面")
                    isLogin = !isLogin!!
                    navController.navigate("MAIN_SCREEN?isLogin=${isLogin}")
                },
                modifier = Modifier
                    .layoutId("LoginButton")
                    .width(150.dp),
            ){
                Text(
                    text = ParkingSharingStrings.ContentDescription.ZH.login,
                )
            }
        }
    }
}

//        ConstraintLayout(constraintSet, modifier = Modifier.fillMaxSize()) {
//            Button(
//                onClick = {},
//                Modifier.layoutId("LoginButton"),
//            ){
//                Text(text = ParkingSharingStrings.ContentDescription.ZH.login)
//            }
//        }
/**
 * 创建一个ConstraintSet实例
 */
//private fun decoupledConstraints(
//    marginTop: Dp = 0.dp,
//    marginStart: Dp = 0.dp,
//    marginBottom: Dp = 0.dp,
//    marginEnd: Dp = 0.dp): ConstraintSet {
//    return ConstraintSet {
//        val idCard = createRefFor("userIDCard")
//
//        constrain(idCard){
//            bottom.linkTo(parent.bottom, margin = marginBottom)
//            end.linkTo(parent.end, margin = marginEnd)
//        }
//    }
//}

